/**
 * Turtles!
 */
Blockly.Blocks["turtle_create"] = {
    init: function() {
        this.appendDummyInput()
            .appendField("create new turtle");
        this.setOutput(true, "Turtle");
        this.setColour(180);
        this.setTooltip("Creates a new turtle");
        this.setHelpUrl("");
    }
};
Blockly.Python["turtle_create"] = function(block) {
    Blockly.Python.definitions_["import_turtle"] = "import turtle";
    var code = "turtle.Turtle()";
    // TODO: Change ORDER_NONE to the correct strength.
    return [code, Blockly.Python.ORDER_NONE];
};
PythonToBlocks.KNOWN_MODULES["turtle"] = {
    "Turtle": ["turtle_create"]
};

Blockly.Blocks["turtle_color"] = {
    init: function() {
        this.appendValueInput("TURTLE")
            .setCheck("Turtle")
            .appendField("make turtle");
        this.appendValueInput("COLOR")
            .setCheck(null)
            .appendField("color");
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setOutput(false);
        this.setInputsInline(true);
        this.setColour(180);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};
Blockly.Python["turtle_color"] = function(block) {
    var turtle = Blockly.Python.valueToCode(block, "TURTLE", Blockly.Python.ORDER_ATOMIC);
    var color = Blockly.Python.valueToCode(block, "COLOR", Blockly.Python.ORDER_ATOMIC);
    var code = turtle+".color("+color+")\n";
    return code;
};
PythonToBlocks.KNOWN_ATTR_FUNCTIONS["color"] = function(func, args, keywords, starargs, kwargs, node) {
    if (args.length < 1 || args.length > 2) {
        throw new Error("Incorrect number of arguments to turtle.color!");
    }
    return [block("turtle_color", func.lineno, {}, { 
        "COLOR": this.convert(args[0]),
        "TURTLE": this.convert(func.value)
    }, {"inline": "true"})];
};

Blockly.Blocks["turtle_forward"] = {
    init: function() {
        this.appendValueInput("TURTLE")
            .setCheck("Turtle")
            .appendField("make turtle");
        this.appendValueInput("DISTANCE")
            .setCheck(null)
            .appendField("move forward by");
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setOutput(false);
        this.setColour(180);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};
Blockly.Python["turtle_forward"] = function(block) {
    var turtle = Blockly.Python.valueToCode(block, "TURTLE", Blockly.Python.ORDER_ATOMIC);
    var distance = Blockly.Python.valueToCode(block, "DISTANCE", Blockly.Python.ORDER_ATOMIC);
    // TODO: Assemble Python into code variable.
    var code = turtle+".forward("+distance+")\n";
    return code;
};
PythonToBlocks.KNOWN_ATTR_FUNCTIONS["forward"] = function(func, args, keywords, starargs, kwargs, node) {
    if (args.length != 1) {
        throw new Error("Incorrect number of arguments to turtle.forward!");
    }
    return [block("turtle_forward", func.lineno, {}, { 
        "DISTANCE": this.convert(args[0]),
        "TURTLE": this.convert(func.value)
    }, {"inline": "true"})];
};

Blockly.Blocks["turtle_backward"] = {
    init: function() {
        this.appendValueInput("TURTLE")
            .setCheck("Turtle")
            .appendField("make turtle");
        this.appendValueInput("DISTANCE")
            .setCheck(null)
            .appendField("move backward by");
        this.setPreviousStatement(true);
        this.setInputsInline(true);
        this.setNextStatement(true);
        this.setOutput(false);
        this.setColour(180);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};
Blockly.Python["turtle_backward"] = function(block) {
    var turtle = Blockly.Python.valueToCode(block, "TURTLE", Blockly.Python.ORDER_ATOMIC);
    var distance = Blockly.Python.valueToCode(block, "DISTANCE", Blockly.Python.ORDER_ATOMIC);
    // TODO: Assemble Python into code variable.
    var code = turtle+".backward("+distance+")\n";
    return code;
};
PythonToBlocks.KNOWN_ATTR_FUNCTIONS["backward"] = function(func, args, keywords, starargs, kwargs, node) {
    if (args.length != 1) {
        throw new Error("Incorrect number of arguments to turtle.backward!");
    }
    return [block("turtle_backward", func.lineno, {}, { 
        "DISTANCE": this.convert(args[0]),
        "TURTLE": this.convert(func.value)
    }, {"inline": "true"})];
};

Blockly.Blocks["turtle_left"] = {
    init: function() {
        this.appendValueInput("TURTLE")
            .setCheck("Turtle")
            .appendField("make turtle");
        this.appendValueInput("ANGLE")
            .setCheck(null)
            .appendField("turn left by");
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setOutput(false);
        this.setColour(180);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};
Blockly.Python["turtle_left"] = function(block) {
    var turtle = Blockly.Python.valueToCode(block, "TURTLE", Blockly.Python.ORDER_ATOMIC);
    var angle = Blockly.Python.valueToCode(block, "ANGLE", Blockly.Python.ORDER_ATOMIC);
    // TODO: Assemble Python into code variable.
    var code = turtle+".left("+angle+")\n";
    return code;
};
PythonToBlocks.KNOWN_ATTR_FUNCTIONS["left"] = function(func, args, keywords, starargs, kwargs, node) {
    if (args.length != 1) {
        throw new Error("Incorrect number of arguments to turtle.left!");
    }
    return [block("turtle_left", func.lineno, {}, { 
        "ANGLE": this.convert(args[0]),
        "TURTLE": this.convert(func.value)
    }, {"inline": "true"})];
};

Blockly.Blocks["turtle_right"] = {
    init: function() {
        this.appendValueInput("TURTLE")
            .setCheck("Turtle")
            .appendField("make turtle");
        this.appendValueInput("ANGLE")
            .setCheck(null)
            .appendField("turn right by");
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setOutput(false);
        this.setColour(180);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};
Blockly.Python["turtle_right"] = function(block) {
    var turtle = Blockly.Python.valueToCode(block, "TURTLE", Blockly.Python.ORDER_ATOMIC);
    var angle = Blockly.Python.valueToCode(block, "ANGLE", Blockly.Python.ORDER_ATOMIC);
    // TODO: Assemble Python into code variable.
    var code = turtle+".right("+angle+")\n";
    return code;
};
PythonToBlocks.KNOWN_ATTR_FUNCTIONS["right"] = function(func, args, keywords, starargs, kwargs, node) {
    if (args.length != 1) {
        throw new Error("Incorrect number of arguments to turtle.right!");
    }
    return [block("turtle_right", func.lineno, {}, { 
        "ANGLE": this.convert(args[0]),
        "TURTLE": this.convert(func.value)
    }, {"inline": "true"})];
};